/*
 * @Author: seelingzheng
 * @Date: 2025-03-16 13:29:03
 * @LastEditors: error: git config user.name & please set dead value or install git
 * @LastEditTime: 2025-03-18 12:01:46
 * @Description: file content
 * @WebSite: http://webgis.fun
 */
/**
 * Export Image plugin
 */
(function (namespace) {
  'use strict';

  namespace.plugins = namespace.plugins || {};

  /**
   * Export Image tool
   * @param {DrawerJs.Drawer} drawerInstance
   * @param {Object} options
   * @constructor
   * @extends {DrawerJs.plugins.BaseTool}
   */
  var ExportImagePlugin = function ExportImagePluginConstructor(drawerInstance, options) {
    DrawerJs.plugins.BaseTool.call(this, drawerInstance, options);
    this.name = 'ExportImage';
    this.btnClass = 'btn-export-image';
    this.faClass =options.faClass|| 'fa fa-cloud-download';
    // tooltipCss 
    drawerInstance.options.tooltipCss=Object.assign({},drawerInstance.options.tooltipCss,{ 
      'min-width': '70px', 
    });
    this.tooltip = drawerInstance.t('Export as Image');
  };

  // Inherit from BaseTool
  ExportImagePlugin.prototype = Object.create(DrawerJs.plugins.BaseTool.prototype);
  ExportImagePlugin.prototype.constructor = ExportImagePlugin;

  /**
   * Creates tool button with custom tooltip
   * @param {DrawerToolbar} toolbar toolbar, where this tool button will be created
   */
  ExportImagePlugin.prototype.createButton = function (toolbar) {
    var buttonConfig = {
      buttonOrder: this.options && this.options.buttonOrder,
      additionalClass: this.btnClass,
      iconClass: this.faClass,
      tooltipText: this.tooltip,
      clickHandler: this.onButtonClick.bind(this)
    };

    this.$toolButton = toolbar.addButton(buttonConfig);
  };

  /**
   * On tool button click - export the canvas as image
   */
  ExportImagePlugin.prototype.onButtonClick = function () {
    var drawer = this.drawerInstance;
    var dataURL = drawer.fCanvas.toDataURL({
      format: 'png',
      quality: 1
    });

    // Create a temporary link element
    var link = document.createElement('a');
    link.href = dataURL;
    link.download = 'canvas-image.png';

    // Trigger download
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  };

  namespace.plugins.ExportImage = ExportImagePlugin;
})(DrawerJs); 